package com.xh.bussiness.tms.controller;

import com.xh.bussiness.system.model.Student;
import com.xh.bussiness.system.service.IStudentService;
import com.xh.bussiness.tms.bean.request.ClassStudentRequest;
import com.xh.bussiness.tms.model.Class;
import com.xh.bussiness.tms.model.ClassStudent;
import com.xh.bussiness.tms.service.IClassManageService;
import com.xh.bussiness.tms.service.IClassStudentService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.util.CacheUtils;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/classStudent")
public class ClassStudentController {

    @Autowired
    private IClassManageService classManageService;
    @Autowired
    private IClassStudentService classStudentService;
    @Autowired
    private IStudentService studentService;

    @ApiOperation(value = "添加班级的学生",notes = "添加班级的学生")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@Validated ClassStudent bean){
        if(bean.getId()==null){
            classStudentService.insert(bean);
        }else{
            classStudentService.update(bean);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",bean);
    }


    @ApiOperation(value = "批量添加班级的学生",notes = "批量添加班级的学生")
    @PostMapping("/batchInsert")
    public AjaxResult batchInsert(ClassStudentRequest bean){
           for(String stuId : bean.getStudentIds()){
                ClassStudent cs = new ClassStudent();
                cs.setClassId(bean.getClassId());
                cs.setStudentId(stuId);
               classStudentService.insert(cs);
           }

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    @ApiOperation(value = "删除班级里的学生或者删除学生的班级",notes = "删除班级里的学生或者删除学生的班级")
    @PostMapping("/deleteStudentByClassIdOrStuId")
    public AjaxResult deleteStudentOrClassByClassIdOrStuId(ClassStudent bean){

        classStudentService.deleteStudentOrClassByClassIdOrStuId(bean);

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    @ApiOperation(value = "查询学生班级",notes = "查询学生班级")
    @PostMapping("/getDataByStuIdAndClassId")
    public AjaxResult getClassStudentDataByStuIdAndClassId(ClassStudent bean){

        List<ClassStudent> data = classStudentService.getClassStudentDataByStuIdAndClassId(bean);

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,data);
    }


    @RequestMapping("/queryClassStudentByUserId")
    public AjaxResult queryClassStudentByUserId(HttpServletRequest request,String subjectId) {
        String userId = request.getHeader("userId");
        List<Class> classes = classManageService.queryClassByTeacherId(userId,subjectId);
        if (CollectionUtils.isEmpty(classes)) {
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS, new ArrayList<>());
        }
        ClassStudent classStudent = new ClassStudent();
        for (Class aClass : classes) {
            classStudent.setClassId(aClass.getId());
            List<ClassStudent> classStudentList = classStudentService.getClassStudentDataByStuIdAndClassId(classStudent);
            if (!CollectionUtils.isEmpty(classStudentList)) {
                List<Student> students = new ArrayList<>();
                for (ClassStudent student : classStudentList) {
                    Student stu = studentService.getById(student.getStudentId());
                    students.add(stu);
                }
                aClass.setStudents(students);
            }
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, classes);
    }

    @RequestMapping("/queryClassByStuId")
    public AjaxResult queryClassByStuId(String studentId) {
        if (StringUtils.isEmpty(studentId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        ClassStudent classStudent = new ClassStudent();
        classStudent.setStudentId(studentId);
        List<ClassStudent> datas = classStudentService.getClassStudentDataByStuIdAndClassId(classStudent);
        if (CollectionUtils.isEmpty(datas)) {
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS, new ArrayList<>());
        }
        List<@NotNull String> ids = datas.stream().map(ClassStudent::getClassId).collect(Collectors.toList());
        List<Class> classes = classManageService.queryClassByIds(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, classes);
    }

}
